package com.example.s;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class GameView extends SurfaceView implements SurfaceHolder.Callback {

	public static int NUMBER_OF_LINE = 8;
	public static int NUMBER_OF_PIECE = 8;
	
	private Paint mLineColor,mColor1,mColor2;
	private float[] startX,startY,stopX,stopY;
	private float radius;
	
	private SurfaceHolder mSurfHold;
	
	private Point[] point;
	
	public GameView(Context context) {
		super(context);
		startX = new float[NUMBER_OF_LINE];
		startY = new float[NUMBER_OF_LINE];
		stopX = new float[NUMBER_OF_LINE];
		stopY = new float[NUMBER_OF_LINE];
		point = new Point[NUMBER_OF_PIECE];
		for(int i = 0; i < NUMBER_OF_PIECE / 2; i++) {
			point[i] = new Point(i,0);
		}
		for(int i = NUMBER_OF_PIECE / 2; i < NUMBER_OF_PIECE; i++) {
			point[i] = new Point((i - 4),NUMBER_OF_LINE / 2 - 1);
		}
		mSurfHold = getHolder();
		mSurfHold.addCallback(this);
		setFocusable(true);
		mLineColor = new Paint();
		mLineColor.setAntiAlias(true);
		mLineColor.setColor(Color.BLUE);
		mColor1 = new Paint();
		mColor1.setAntiAlias(true);
		mColor1.setColor(Color.BLACK);
		mColor2 = new Paint();
		mColor2.setAntiAlias(true);
		mColor2.setColor(Color.WHITE);
		
	}
	
	public void draw(Canvas canvas) {
		super.draw(canvas);
		for(int i = 0; i < NUMBER_OF_LINE / 2;i++) {
			canvas.drawLine(startX[i], startY[i], stopX[i], stopY[i], mLineColor);
		}
		for(int i = NUMBER_OF_LINE / 2 ;i < NUMBER_OF_LINE; i++) {
			canvas.drawLine(startX[i], startY[i], stopX[i], stopY[i], mLineColor);	
		}
		for(int i = 0; i < NUMBER_OF_PIECE / 2; i++) {
			canvas.drawCircle(startX[point[i].x + 4], startY[point[i].y] , radius, mColor1);
		}
		for(int i = NUMBER_OF_PIECE / 2; i < NUMBER_OF_PIECE; i++) {
			canvas.drawCircle(startX[point[i].x + 4], startY[point[i].y] , radius, mColor2);
		}
	}

	@Override
	public void surfaceChanged(SurfaceHolder holder, int format, int width,
			int height) {
		this.setBackgroundResource(R.drawable.bg);
		radius = Math.min(width,height) / 15;
		for(int i = 0; i < NUMBER_OF_LINE / 2;i++) {
			startX[i] = width * 5 / 50;
			stopX[i] = width * 35 / 50;
			startY[i] = height * i / 5 + height / 10;
			stopY[i] = startY[i];
		}
		for(int i = NUMBER_OF_LINE / 2 ;i < NUMBER_OF_LINE;i++) {
			startX[i] = width * (i - 4) / 5 + width / 10;
			stopX[i] = startX[i];
			startY[i] = height * 5 / 50;
			stopY[i] = height * 35 / 50;
		}
		Canvas c = null;
		try{
			c = mSurfHold.lockCanvas();
			synchronized(mSurfHold){
				draw(c);
			}
		} finally {
			if(c != null)
				mSurfHold.unlockCanvasAndPost(c);
		}
	}

	@Override
	public void surfaceCreated(SurfaceHolder holder) {
		
	}

	@Override
	public void surfaceDestroyed(SurfaceHolder holder) {
		// TODO Auto-generated method stub
		
	}


}
